package com.ke.forum.forumapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ke.forum.common.common.Result;
import com.ke.forum.mbg.dao.UserMapper;
import com.ke.forum.mbg.entity.Article;
import com.ke.forum.mbg.dao.ArticleMapper;
import com.ke.forum.forumapi.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ke.forum.mbg.entity.dto.ArticleDto;
import com.ke.forum.mbg.entity.dto.UserDto;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 珂
 * @since 2022-05-22
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public Result addArticle(Article article) {
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        article.setId(null);
        article.setIsRare(0);
        article.setCreateTime(LocalDateTime.now());
        article.setIsExamine(1);
        article.setIsTop(0);
        article.setViews(0);
        article.setStatus(1);
        article.setUpdateTime(LocalDateTime.now());
        article.setUserUuid(userDto.getUuid());
        articleMapper.insert(article);
        return Result.SUCCESS(true);
    }

    @Override
    public Result updateArticle(Article article) {
        return null;
    }

    @Override
    public Result deleteArticle(Integer id) {
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        Article article = articleMapper.selectById(id);
        if (!article.getUserUuid().equals(userDto.getUuid())){
            return Result.FAIL("不可删除他人文章");
        }
        articleMapper.deleteById(id);
        return Result.SUCCESS(true);
    }

    @Override
    public Result listArticle(Integer sectionId, Boolean isRare, Integer sortWay, Integer current) {
        Page<ArticleDto> page = new Page<>();
        page.setCurrent(current);
        page.setSize(10);
        Page<ArticleDto> articleDtoPage = null;
        if (isRare){
            if (sortWay==1){
                articleDtoPage = articleMapper.listRareSortByCreatetime(page, sectionId);
            }else if (sortWay==2) {
                articleDtoPage = articleMapper.listRareSortByViews(page, sectionId);
            }
        }else {
            if (sortWay==1){
                articleDtoPage = articleMapper.listArticleSortByCreatetime(page, sectionId);
            }else if (sortWay==2) {
                articleDtoPage = articleMapper.listArticleSortByViews(page, sectionId);
            }
        }
        return Result.SUCCESS(articleDtoPage);
    }

    @Override
    public Result getArticleInfo(Integer id) {
        ArticleDto articleInfo = articleMapper.getArticleInfo(id);
        return Result.SUCCESS(articleInfo);
    }

    @Override
    public Result listArticleByUserId(String uuid,Integer current) {
        if (uuid==null){
            UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
            uuid = userDto.getUuid();
        }
        Page<ArticleDto> page = new Page<>();
        page.setSize(10);
        page.setCurrent(current);
        Page<ArticleDto> articleDtoPage = articleMapper.listArticleByUserUuid(page, uuid);
        return Result.SUCCESS(articleDtoPage);
    }

    @Override
    public Result listTopArticle(Integer sectionId) {
        List<ArticleDto> articleDtos = articleMapper.listTopArticle(sectionId);
        return Result.SUCCESS(articleDtos);
    }

    @Override
    public Result listStarUserArticles(Integer current) {
        Page<ArticleDto> page = new Page<>();
        page.setCurrent(current);
        page.setSize(10);
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        Page<ArticleDto> articleDtoPage = articleMapper.listStarUserArticles(page, userDto.getUuid());
        return Result.SUCCESS(articleDtoPage);
    }
}
